package main

import (
	"flag"
	"fmt"
	"math/rand"
)

var (
	lenth   int
	charset string
)

const (
	numStr       = "1234567890"
	charStr      = "qwertyuiopasdfghjklzxcvbnm"
	characterStr = "!@#$%^&*"
)

func parseArgs() {
	flag.IntVar(&lenth, "l", 16, "l密码长度")
	flag.StringVar(&charset, "t", "num",
		`-t 字符集，num:仅仅使用数字，char:仅仅使用字母，mix:数字和字母，advance:数字字母以及下划线`)
	flag.Parse()
}

func main() {
	parseArgs()
	fmt.Printf("lenth:%v,charset:%v\n", lenth, charset)
	password := password()
	fmt.Println(password)
}
func password() string {
	password := make([]byte, lenth, lenth)
	var sourceStr string
	switch charset {
	//case "num":
	//	sourceStr = numStr
	case "char":
		sourceStr = charStr
	case "mix":
		sourceStr = fmt.Sprintf("%s%s", numStr, charStr)
	case "advance":
		sourceStr = fmt.Sprintf("%s%s%s", numStr, charStr, characterStr)
	default:
		sourceStr = numStr
	}
	for i := 0; i < lenth; i++ {
		index := rand.Intn(len(sourceStr))
		password[i] = sourceStr[index]
	}
	return string(password)
}
